#ifndef CLASSROOMFILEREADER_H_INCLUDED
#define CLASSROOMFILEREADER_H_INCLUDED
#include "../Entities/classroom.h"
#include "stringtokenizer.h"
#include <fstream>
#include <stdlib.h>
#include <iostream>
namespace ClassroomFileReader{
    typedef std::vector<Classroom> ClassroomList;

    ClassroomList read(const char* filename){
        ClassroomList ret;
        std::ifstream file(filename, std::ios::in);
        if(!file.is_open())
            throw "file cannot be loaded.";
        int id = 1;
        while(!file.eof()){
            char line[500];
            file.getline(line, 500);
            StringTokenizer tokenizer(line,"%");

            if(!tokenizer.hasMoreTokens())
                throw "invalid file format.";
            std::string name = tokenizer.nexToken();

            if(!tokenizer.hasMoreTokens())
                throw "invalid file format";
            try{
                int capacity = atoi(tokenizer.nexToken().c_str());

                Classroom a(name.c_str(), capacity, id++);
                ret.push_back(a);
            } catch(...){
                throw "invalid file format.";
            }

        }
        file.close();
        return ret;
    }
}


#endif // CLASSROOMFILEREADER_H_INCLUDED
